// pages/student/favorite/favorite.js
import { courses, books } from '../../../utils/mockData.js'

Page({
  data: {
    tabIndex: 0,
    tabs: ['课程', '资料'],
    favoriteCourses: [],
    favoriteBooks: [],
    loading: true
  },
  onLoad() {
    this.loadFavorites()
  },
  loadFavorites() {
    wx.showLoading({ title: '加载中...' })
    setTimeout(() => {
      this.setData({
        favoriteCourses: courses.slice(0, 2),
        favoriteBooks: books,
        loading: false
      })
      wx.hideLoading()
    }, 500)
  },
  switchTab(e) {
    const index = e.currentTarget.dataset.index
    this.setData({ tabIndex: index })
  },
  goToCourse(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/student/course-detail/course-detail?id=${id}`
    })
  },
  goToBook(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/student/book-detail/book-detail?id=${id}`
    })
  },
  removeFavorite(e) {
    const { id, type } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '确定要取消收藏吗？',
      success: (res) => {
        if (res.confirm) {
          if (type === 'course') {
            const courses = this.data.favoriteCourses.filter(item => item.id !== id)
            this.setData({ favoriteCourses: courses })
          } else {
            const books = this.data.favoriteBooks.filter(item => item.id !== id)
            this.setData({ favoriteBooks: books })
          }
          wx.showToast({ title: '已取消收藏', icon: 'success' })
        }
      }
    })
  }
})
